package test20230430;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/4/30 2:52
 * 坦克游戏
 */
public class Fight {

    public static void main(String[] args) {
        // 创建两个坦克
        Tank t1 = new Tank();
        Tank t2 = new Tank();
        // 初始炮弹数量均为10 并展示
        t1.setBulletAmount(10);
        t2.setBulletAmount(10);
        System.out.println("tank1炮弹的数量: " + t1.getBulletAmount());
        System.out.println("tank2炮弹的数量: " + t2.getBulletAmount());
        // t1 加速到 80, t2 加速到 90, 并展示
        System.out.print("tank1 ");
        t1.speedUp(80);
        System.out.print("tank2 ");
        t2.speedUp(90);
        // t1 减速到 65, t2 减速到 60
        System.out.print("tank1 ");
        t1.speedDown(15);
        System.out.print("tank2 ");
        t2.speedDown(30);
        // 模拟开火
        System.out.println("tank1开火: ");
        t1.fire();
        System.out.println("tank2开火: ");
        t2.fire();
        t2.fire();
        // 打印剩余炮弹数量
        System.out.println("tank1炮弹的数量: " + t1.getBulletAmount());
        System.out.println("tank2炮弹的数量: " + t2.getBulletAmount());
    }
}

// 坦克类
class Tank {
    private double speed; // 坦克的速度
    private int bulletAmount; // 坦克的炮弹的个数

    // 加速
    public void speedUp(double upSpeed) {
        this.speed += upSpeed;
        System.out.println("目前的速度为:" + String.format("%.1f", speed));
    }

    // 减速
    public void speedDown(double downSpeed) {
        this.speed -= downSpeed;
        System.out.println("目前的速度为:" + String.format("%.1f", speed));
    }

    // 模仿坦克开火
    public void fire() {
        if (this.bulletAmount <= 0) {
            System.out.println("当前炮弹数量不足~");
            return;
        }
        bulletAmount--;
        System.out.println("打出一发炮弹");
    }

    public double getSpeed() {
        return speed;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }

    public int getBulletAmount() {
        return bulletAmount;
    }

    public void setBulletAmount(int bulletAmount) {
        this.bulletAmount = bulletAmount;
    }
}
